package com.ofps.web.controller.forms;

import com.ofps.common.core.domain.entity.SysUser;
import com.ofps.forms.common.utils.R;
import com.ofps.forms.domain.Dto.UserDto;
import com.ofps.forms.domain.MyUser;
import com.ofps.forms.service.MyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pyn
 * @date 2022/5/6 - 21:15
 */
@RestController
@RequestMapping("form/user")
public class MyUserController {
    @Autowired
    private MyUserService myUserService;

    @RequestMapping("/login")
    @RequiresPermissions("user:login")
    public R login(@RequestBody UserDto userDto) {
        return myUserService.login(userDto);
    }


    @RequestMapping("/info")
    @RequiresPermissions("user:info")
    public R info(HttpServletRequest request) {
        return myUserService.getInfo(request);
    }


    //注册
    @RequestMapping("/register")
    @RequiresPermissions("user:register")
    public R register(@RequestBody MyUser myUser) {
        return myUserService.register(myUser);
    }

    @RequestMapping("/logout")
    @RequiresPermissions("user:logout")
    public R logout() {
        return R.ok("注销登录成功");
    }

    @RequestMapping("/queryWithDeptId")
    public R queryWithDeptId(@RequestBody Map<String,Object> body){
        List<Map<String,Object>> messages = (List<Map<String,Object>>) body.get("depts");
        List<Object> RetMessage = new ArrayList<>();
        for(Map<String,Object> message :  messages){
            Integer deptid =(Integer)message.get("deptId");
            Long deptId = deptid.longValue();
            String deptName = message.get("deptName").toString();
            List<SysUser> users = myUserService.queryWithDeptId(deptId);
            for (SysUser user : users){
                Map<String,String> retMap = new HashMap<>();
                System.out.println(user);
                retMap.put("deptName",deptName);
                retMap.put("user_id",user.getUserId().toString());
                retMap.put("user_name",user.getUserName());
                retMap.put("nick_name",user.getNickName());
                RetMessage.add(retMap);
            }
        }
        if (null == RetMessage)
            return R.ok("暂无信息!");
        else
            return R.ok("查询成功!").put("retMessage",RetMessage);
    }
}
